<?php
class ProductAction extends Action{
       private $table;//表名称
	   private $pk;//主键
	   private $col_name;//栏目名称
	   private $col_id;//栏目id
	   private $is_search;//是否需要搜索框
	   private $is_sequence;//是否包含字段sequence
	   private $mult_opt_fields;//复选框字段 
	   private $tablefill_dropdown; //数据表字段填充  下拉框  列表显示
	   private $input_field_arr; //文本框搜索字段
	   private $dropdown_field_arr;//下拉框搜索字段
	   private $syscolumn;//栏目对象
	   private $sysfield;//字段表对象
	   public $model;//对应数据表对象
	   private $map;//搜索条件
	   public $divpage_param;//分页条件
	   public $redirect_param;//跳转条件
	   private $preview_url;//预览网址
	   
       public function _initialize(){
	      header("content-type:text/html;charset=utf-8");
		  //验证和初始化一些模型
	      check_pression();//检查权限
		  $this->syscolumn=M("syscolumn");//栏目对象
		  $this->sysfield=M("sysfield"); //字段表对象	 
		  //表
		  $this->table=$this->getActionName();
		  $this->assign("table",$this->table);
		  $this->model=D($this->table); //对应数据表对象
		  //主键
		  $this->pk=$this->model->getPk();
		  $this->assign("pk",$this->pk);
		  //栏目名称
		  $this->col_name=$this->syscolumn->where("col_eng_name='$this->table'")->getField("col_name");
		  $this->assign("col_name",$this->col_name);
		  //预览网址
		  $this->preview_url=$this->syscolumn->where("col_eng_name='$this->table'")->getField("preview_url");
		  $this->assign("preview_url",$this->preview_url);
		  //栏目id
		  $this->col_id=$this->syscolumn->where("col_eng_name='$this->table'")->getField("id");
		  $this->assign("col_id",$this->col_id);
		  //是否包含字段sequence 
		  $table_field_arr=$this->model->getDbFields();
		  $this->is_sequence=array_search("sequence",$table_field_arr,true);
		  $this->assign("is_sequence",$this->is_sequence);
		  //是否需要搜索框
		  $this->is_search=$this->syscolumn->where("col_eng_name='$this->table'")->getField("is_search");
		  $this->assign("is_search",$this->is_search);
		  //搜索字段
		  $search_field=$this->syscolumn->where("col_eng_name='$this->table'")->getField("keyword_field");
		  if(strpos($search_field,"|")){
		     //文本框搜索字段
		     $input_field=array_shift(explode("|",$search_field));
		     $this->input_field_arr=explode(",",$input_field);
		     //下拉框搜索字段
		     $dropdown_field=end(explode("|",$search_field));
		     $this->dropdown_field_arr=explode(",",$dropdown_field);
		     $this->assign("dropdown_field_arr",$this->dropdown_field_arr);
		  }else{
			 $this->input_field_arr=explode(",",$search_field); 
		  }
		  //关键字描述
		  if(!empty($this->input_field_arr)){
              foreach($this->input_field_arr as $k=>$v){
				 $keyword_tip_arr[]=$this->sysfield->where("field_name='$v' and col_eng_name='$this->table'")->getField("field_comment");//2010-12-20 lijianwei 修改bug 如果有相同字段，提示错误，加上col_eng_name='$this->table'即可.
			  }
			  if(!empty($keyword_tip_arr))$this->assign("keyword_tip",implode(",",$keyword_tip_arr));
		  }
		  //复选框
		  $mult_opt_fields=$this->sysfield->where("field_output_type='复选框' and col_eng_name='$this->table'")->field("field_name")->findAll();
		  if(!empty($mult_opt_fields)){
		    $arr=array();
		    foreach($mult_opt_fields as $k=>$v){
		      array_push($arr,$v['field_name']);
		    }
		    $this->mult_opt_fields=$arr;
		  }
		  //数据表字段填充  下拉框
		  $tablefill_dropdown=$this->sysfield->where("(field_output_type='下拉框' or field_output_type='二级下拉联动') and col_eng_name='$this->table' and field_value_fill_type='数据表字段填充'")->field("field_name,field_value_fill,field_output_type")->findAll();
		  $this->tablefill_dropdown=$tablefill_dropdown;
		  //分页
		  $nowpage=$_GET["p"]?$_GET["p"]:1;
		  $divpage_param_arr["p"]=$nowpage;
		  //每页记录数
		  $sys_pagesize=$_GET["sys_pagesize"];
		  if(!empty($sys_pagesize)) $divpage_param_arr["sys_pagesize"]=$sys_pagesize;
          
		  //查询条件
		  $keyword=($_POST["ljw_search"]=="1")?$_POST["keyword"]:base64_decode($_GET["keyword"]);
		  if(!empty($keyword)) $divpage_param_arr["keyword"]=base64_encode($keyword);
          //文本框搜索条件
		  if(!empty($this->input_field_arr)&&!empty($keyword)){
		     foreach($this->input_field_arr as $v){
			   $input_map[$v]=array("like","%$keyword%");
			 }
			 $input_map["_logic"]="or";
		  }
		  //下拉框搜索条件 或 单选搜索条件
		  if(!empty($this->dropdown_field_arr)){
		      foreach($this->dropdown_field_arr as $v){
			     $dropdown_value=($_POST["ljw_search"]=="1")?$_POST[$v]:base64_decode($_GET[$v]);
		         $dropdown_value=$dropdown_value?$dropdown_value:0;
				 if(!empty($dropdown_value)){ 
				     $field_value_fill=$this->sysfield->where("col_eng_name='$this->table' and field_name='$v'")->getField("field_value_fill");
					 $field_value_fill_arr=explode("@",$field_value_fill);
					 if($field_value_fill_arr[0]=="sysclass"){//多级下拉类别
						 $col_id=end(explode("=",$field_value_fill_arr[3]));
						 if(empty($col_id))$col_id=$this->col_id;
						 $dropdown_map[$v]=array("in",typeid2child($dropdown_value,"sysclass",$col_id));
					 }else{//单级下拉 或 单选
						 $dropdown_map[$v]=$dropdown_value;
					 }
					 $divpage_param_arr[$v]=base64_encode($dropdown_value);
				 }
			  }
			  //if(!empty($dropdown_map))$dropdown_map["_logic"]="and";//修改 lijianwei  如果没有下拉框，查询条件出错
		  }
		  if(empty($input_map)){
		     $map=$dropdown_map;
		  }else{
		     if(!empty($dropdown_map)){
			    $dropdown_map["_complex"]=$input_map;
				$map=$dropdown_map;
			 }else{
			    $map=$input_map;
			 }
		  }
		  $this->map=$map;
		  $this->redirect_param=array_map("urlencode",$divpage_param_arr);
		  $this->assign("redirect_param",$this->redirect_param);
		  foreach($this->redirect_param as $k=>$v){
              $redirect_p[]=$k;
			  $redirect_p[]=$v;
		  }
		  $this->assign("divpage_param",implode("/",$redirect_p));
	   }
	   public function index(){
		  //求出 字段数组  comment数组
		  $field_arr=$this->sysfield->where("col_eng_name='$this->table' and field_islist=1 and field_isuse=1")->field("field_name,field_comment,field_index_size")->order("sequence desc")->findAll();
		  $field_name_arr=array();
		  $field_comment_arr=array();
		  $field_index_size_arr=array();
		  foreach($field_arr as $k=>$v){
		     array_push($field_name_arr,$v["field_name"]);
			 array_push($field_comment_arr,$v["field_comment"]);
			 if(!empty($v['field_index_size']))$field_index_size_arr[$v['field_name']]=$v['field_index_size'];
		  }
		  //$this->assign("field_name_arr",$field_name_arr);//不包含主键id
		  if($this->is_sequence){
		     $key=array_search("sequence",$field_name_arr);
		     unset($field_comment_arr[$key]);  
		  }
		  $this->assign("field_comment_arr",$field_comment_arr);
		  array_push($field_name_arr,$this->pk);
		  $field_name_str=implode(",",$field_name_arr);//包含主键
		  
		  //分页
		  $totalnum=$this->model->where($this->map)->count();
		  Vendor("LJW.page");
		  import("ORG.Cons");
		  $sys_pagesize=$_GET["sys_pagesize"]?intval($_GET["sys_pagesize"]):Cons::$sysconfig["pagenum"];
		  $this->assign("sys_pagesize",$sys_pagesize);
		  $p=new page($totalnum,$sys_pagesize,0,U('index',$this->redirect_param));
		  //$p->parameter=$this->divpage_param;
		  $page=$p->show();
		  if($this->is_sequence){
		      $list=$this->model->where($this->map)->field($field_name_str)->limit($p->start.",".$p->pagesize)->order("sequence desc")->findAll();
			  //修正排序
			  $seq_list=$this->model->where($this->map)->order("sequence desc")->findAll();
			  $num=count($seq_list);
			  $this->assign("maxseq",$seq_list[0]["sequence"]);
			  $this->assign("minseq",$seq_list[$num-1]["sequence"]);

			  //去除sequence 
              $key=array_search("sequence",$field_name_arr);
			  unset($field_name_arr[$key]);
			  $key=array_search($this->pk,$field_name_arr);
			  unset($field_name_arr[$key]);
		  }else{
		      $list=$this->model->where($this->map)->field($field_name_str)->limit($p->start.",".$p->pagesize)->order($this->pk." desc")->findAll();
			  //$list=array_reverse($oldlist);
			  $key=array_search($this->pk,$field_name_arr);
			  unset($field_name_arr[$key]);  
		  }
		  $this->assign("field_name_arr",$field_name_arr);
		  foreach($list as $k=>$v){
			  //数据表字段填充
			  if(!empty($this->tablefill_dropdown)){
		         foreach($this->tablefill_dropdown as $x=>$y){
				       $field_name=$y["field_name"];
					   $field_value_fill=$y["field_value_fill"];
					   $field_value_fill_arr=explode("@",$field_value_fill);
					   if(empty($field_value_fill_arr)) continue;
					   
					   //处理二级下拉联动
					   if($y["field_output_type"]=="二级下拉联动"){
						 $fill_value_fill_arr=explode("|||",$field_value_fill);
						 $child_value_fill_arr=explode("@",$fill_value_fill_arr[1]);
						 $child_model=M($child_value_fill_arr[0]);
						 $old_value=$list[$k][$field_name];
						 $list[$k][$field_name]=$child_model->where($child_value_fill_arr[1]."=$old_value")->getField($child_value_fill_arr[2]);
						 continue;   
					   }
					   
					   
					   if($field_value_fill_arr[0]=="sysclass"){
					      $list[$k][$field_name]=typestr($v[$field_name],"sysclass",$field_value_fill_arr[3],"class_name");
					   }else{
					      $model=M($field_value_fill_arr[0]);
					      $pk_value=$list[$k][$field_name];
						  if(empty($field_value_fill_arr[3])){
					         $value=$model->where("$this->pk=$pk_value")->getField($field_value_fill_arr[2]);
						  }else{
							  $value=$model->where("$this->pk=$pk_value and $field_value_fill_arr[3]")->getField($field_value_fill_arr[2]); 
						  }
				          $list[$k][$field_name]=$value;
					   }
				 }
			  }
			  //加上截取字符
			  if(!empty($field_index_size_arr)){
				foreach($field_index_size_arr as $m=>$n){
					$list[$k][$m]="<span title='".$list[$k][$m]."'>".cut_str($list[$k][$m],$n)."</span>";
				}
			  }
		  }
		  $this->assign("list",$list);
		  $this->assign("page",$page);
		  //其他操作
		  $action_list=$this->syscolumn->where("col_eng_name='$this->table'")->getField("action_field");
		  if(!empty($action_list)){
		     $action_list_arr=explode(",",$action_list);
		     foreach($action_list_arr as $k=>$v){
			     $child_arr=explode("|",$v);
			     $sys_action[$k]["action_name"]=$child_arr[0];
			     $sys_action[$k]["action_function"]=$child_arr[1];  
		     }
			 $this->assign("sys_action",$sys_action);
		  }
		  $this->display();
	   }
	   public function add(){
		  $field_arr=$this->sysfield->where("col_eng_name='$this->table' and field_isdetail=1 and field_isuse=1")->field("field_name,field_group_id")->order("sequence desc")->findAll();
		  $field_name_arr=array();
		  $group_id_arr=array();
		  $group_flag=0;
		  foreach($field_arr as $k=>$v){
		     array_push($field_name_arr,$v["field_name"]);
			 if(!empty($v['field_group_id'])) $group_flag=1;
			 array_push($group_id_arr,$v['field_group_id']);
		  }
		  $this->assign("field_name_arr",$field_name_arr);
		  //修改 lijianwei 新增字段分组
		  if($group_flag){//说明有分组
		     $unique_group_arr=array_unique($group_id_arr);
			 $key=array_search(0,$unique_group_arr);
			 $sysfield_group=M("sysfield_group");
			 $group_map["id"]=array("in",$unique_group_arr);
			 $group_list=$sysfield_group->where($group_map)->order("sequence desc")->field("id,group_name")->findAll();
			 if(false!==$key) array_unshift($group_list,array("id"=>0,"group_name"=>"基本属性"));
			 foreach($group_list as $k=>$v){
				 $seq_group_arr[]=$v['id'];
			 }
			 foreach($field_name_arr as $k=>$v){
				  $group_id=$group_id_arr[$k];
				  $key=array_search($group_id,$seq_group_arr);
				  $group_list[$key]['fields'][]=$v;
			 }
			 $this->assign("group_list",$group_list);
		  }
		  $this->display();
	   }
	   public function insert(){
		  if(!empty($this->mult_opt_fields)){
		      foreach($this->mult_opt_fields as $k=>$v){
			     if(isset($_POST[$v]))
				   $_POST[$v]=implode(",",$_POST[$v]);
				 else
				   $_POST[$v]="";
			  }
		  }
		  if($this->is_sequence){
		     $maxseq=$this->model->max("sequence");
			 $maxseq++;
			 $_POST["sequence"]=$maxseq;
		  }
		  if($vo=$this->model->create()){
		     $list=$this->model->add();
			 if(false!==$list){
			    $this->success("操作成功");
			 }else{
			    $this->error("操作失败");
			 }
		  }else{
		     $this->error($this->model->geterror());
		  }
	   }
	   public function edit(){
		  $pk_value=$_GET[$this->pk];
		  $this->assign("pk_value",$pk_value);
		  $field_arr=$this->sysfield->where("col_eng_name='$this->table' and field_isdetail=1 and field_isuse=1")->field("field_name,field_group_id")->order("sequence desc")->findAll();
		  $field_name_arr=array();
		  $group_id_arr=array();
		  $group_flag=0;
		  
		  foreach($field_arr as $k=>$v){
		     array_push($field_name_arr,$v["field_name"]);
			 if(!empty($v['field_group_id'])) $group_flag=1;
			 array_push($group_id_arr,$v['field_group_id']);
		  }
		  $this->assign("field_name_arr",$field_name_arr);
		   //修改 lijianwei 新增字段分组
		  if($group_flag){//说明有分组
		     $unique_group_arr=array_unique($group_id_arr);
			 $key=array_search(0,$unique_group_arr);
			 $sysfield_group=M("sysfield_group");
			 $group_map["id"]=array("in",$unique_group_arr);
			 $group_list=$sysfield_group->where($group_map)->order("sequence desc")->field("id,group_name")->findAll();
			 if(false!==$key) array_unshift($group_list,array("id"=>0,"group_name"=>"基本属性"));
			 foreach($group_list as $k=>$v){
				 $seq_group_arr[]=$v['id'];
			 }
			 foreach($field_name_arr as $k=>$v){
				  $group_id=$group_id_arr[$k];
				  $key=array_search($group_id,$seq_group_arr);
				  $group_list[$key]['fields'][]=$v;
			 }
			 $this->assign("group_list",$group_list);
		  }
		  $this->display();  
	   }
	   public function update(){
		   if(!empty($this->mult_opt_fields)){
		      foreach($this->mult_opt_fields as $k=>$v){
			     if(isset($_POST[$v]))
				   $_POST[$v]=implode(",",$_POST[$v]);
				 else
				   $_POST[$v]="";
			  }
		   }
		   if($vo=$this->model->create()){
		       $list=$this->model->save();
			   if(false!==$list){
			      $this->redirect("index",$this->redirect_param,1,'操作成功');
			   }else{
			      $this->error("操作失败");
			   }
		   }else{
		      $this->error($this->model->geterror());
		   }
	   }
	   public function delete(){
		   $pk_value=$_GET[$this->pk];
		   $this->model->startTrans();
		   if($this->model->where("$this->pk=$pk_value")->delete()){
		       $this->model->commit();
			   $this->redirect("index",$this->redirect_param,1,'操作成功');
		   }else{
		       $this->model->rollback();
			   $this->error("操作失败");
		   }
	   }
 public function up(){
	    $pk_value=$_GET[$this->pk];
		$nowseq=$this->model->where("$this->pk=$pk_value")->getField("sequence");

		//修改 lijianwei  如果没有下拉框，查询条件出错
		$seq_map["sequence"]=array("gt",$nowseq);
		if(!empty($this->map))$seq_map["_complex"]=$this->map;
		$next=$this->model->where($seq_map)->order("sequence asc")->find();

		$nextid=$next[$this->pk];
		$nextseq=$next["sequence"];
		
		$this->model->where("$this->pk=$pk_value")->setField("sequence",$nextseq);
		$this->model->where("$this->pk=$nextid")->setField("sequence",$nowseq);
		$this->redirect("index",$this->redirect_param);
	 }
	 
	 public function down(){
        $pk_value=$_GET[$this->pk];
		$nowseq=$this->model->where("$this->pk=$pk_value")->getField("sequence");

        //修改 lijianwei  如果没有下拉框，查询条件出错
		$seq_map["sequence"]=array("lt",$nowseq);
		if(!empty($this->map))$seq_map["_complex"]=$this->map;
		$next=$this->model->where($seq_map)->order("sequence desc")->find();

		$nextid=$next[$this->pk];
		$nextseq=$next["sequence"];
		
		$this->model->where("$this->pk=$pk_value")->setField("sequence",$nextseq);
		$this->model->where("$this->pk=$nextid")->setField("sequence",$nowseq);
		$this->redirect("index",$this->redirect_param);
	 }
	  public function delall(){//删除多选
	    $del_id=$_GET["item_ids"];
		if(empty($del_id)) $this->error("出现错误");
		$del_arr=explode(",",$del_id);

		$this->model->startTrans();
		foreach($del_arr as $k=>$v){
		  if(!$this->model->where("$this->pk=$v")->delete()){
		      $this->model->rollback();
			  $this->error("操作失败");
		  }
		}
		$this->model->commit();
		$this->redirect("index",$this->redirect_param,1,'操作成功');
	 }
	 //空操作
	 public function _empty($name,$arguments){
		 check_pression();
		 if(empty($name)) die("出现错误");
		 require_cache(APP_NAME."/Common/Admin/diy_action.php");
		 if(function_exists($name)){
			 call_user_func_array($name,array(&$this));
		 }else{
			throw_exception('函数'.$name."未找到");
		 }
	 }
	 public function copydata(){
		checkget();
		$pk_value=$_GET[$this->pk];
		if(empty($pk_value)||!is_numeric($pk_value)) alert('出现错误');
		
		$map[$this->pk]=$pk_value;
		$row_value=$this->model->where($map)->find();
		//去除主键值
		unset($row_value[$this->pk]);
		if($this->is_sequence){
			$max_sequence=$this->model->max("sequence");
			$row_value["sequence"]=++$max_sequence;
		}
		if($this->model->add($row_value)){
		   $this->redirect("index");	
		}else{
		   $this->error("复制失败");
		}
	 }
}
?>